﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Domain = RevClinic.Core.Domain;
using NHibernate.Criterion;
using NHibernate.Transform;
using NHibernate.SqlCommand;

namespace RevClinic.Raporting.Impl
{
    public class UserRaportingService : IUserRaportingService
    {
        private ISession _session;

        public UserRaportingService(ISessionFactory fatory)
        {
            _session = fatory.GetCurrentSession();
        }

        public User GetById(long id)
        {
            ICriteria criteria = _session.CreateCriteria<Domain.User>("User")
                .CreateAlias("Group", "Group", JoinType.LeftOuterJoin)
                .Add(Expression.Eq("User.Id", id))
                .SetProjection(Projections.ProjectionList()
                    .Add(Projections.Property("User.Id"), "Id")
                    .Add(Projections.Property("Info.Login"), "Login")
                    .Add(Projections.Property("Info.FirstName"), "FirstName")
                    .Add(Projections.Property("Info.Surname"), "Surname")
                    .Add(Projections.Property("Group.Id"), "GroupId")
                    .Add(Projections.Property("Group.Name"), "GroupName")
                    .Add(Projections.Property("User.IsActive"), "IsActive"))
                .SetResultTransformer(Transformers.AliasToBean<User>());

            return criteria.UniqueResult<User>();
        }

        public IList<User> GetUsers()
        {
            throw new NotImplementedException();
        }
    }
}
